﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace SendHeart
{
    public class MyConvert
    {
        static object WriteLogLock = new object();

        /// <summary>
        /// 字符串转化byte数组
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        static public byte[] StringToByteArray(string str)
        {
            return System.Text.Encoding.Default.GetBytes(str);
        }

        /// <summary>
        /// byte数据，转化为字符串
        /// </summary>
        /// <param name="byteArray"></param>
        /// <returns></returns>
        static public string ByteArrayToString(byte[] byteArray)
        {
            return System.Text.Encoding.Default.GetString(byteArray);
        }

        /// <summary>
        /// byte数据，转化为字符串
        /// </summary>
        /// <param name="byteArray"></param>
        /// <param name="len">需要转换的字节数</param>
        /// <returns></returns>
        static public string ByteArrayToString(byte[] byteArray, int len)
        {
            byte[] buf = new byte[len];
            for (int i = 0; i < len; i++)
            {
                buf[i] = byteArray[i];
            }
            return System.Text.Encoding.Default.GetString(buf);
        }

        /// <summary>
        /// 将字符串转为Ascii 的十六进制
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        static public byte[] StringToAsciiByte(string str)
        {
            return System.Text.Encoding.ASCII.GetBytes(str);
        }

        /// <summary>
        /// 将Ascii的十六进制数组转化为字符串
        /// </summary>
        /// <param name="byteArray"></param>
        /// <returns></returns>
        static public string AsciiByteToString(byte[] byteArray)
        {
            return System.Text.Encoding.ASCII.GetString(byteArray);
        }

        /// <summary>
        /// 将Ascii的十六进制数组转化为字符串
        /// </summary>
        /// <param name="byteArray"></param>
        /// <param name="len">需要转换的字节数</param>
        /// <returns></returns>
        static public string AsciiByteToString(byte[] byteArray, int len)
        {
            byte[] buf = new byte[len];
            for (int i = 0; i < len; i++)
            {
                buf[i] = byteArray[i];
            }
            return System.Text.Encoding.ASCII.GetString(buf);
        }

        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="logStr"></param>
        static public void WriteLog(string logStr)
        {
            lock (WriteLogLock)
            {
                DirectoryInfo dir = new DirectoryInfo("log");
                dir.Create();//自行判断一下是否存在。
                string FileName = "log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
                FileStream fs = new FileStream(FileName, FileMode.Append);//如果没有该文件即创建，如果存在，即打开并在末尾添加内容
                string str1 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "   ";
                //获得字节数组
                byte[] data = Encoding.Default.GetBytes(str1 + logStr + "\r\n");
                //开始写入
                fs.Write(data, 0, data.Length);
                //清空缓冲区、关闭流
                fs.Flush();
                fs.Close();
            }
        }
    }
}
